<?php
/**
 * Abstract.php
 * 
 * @category   Naneau
 * @package    Naneau_Images
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1
 */

/**
 * Naneau_Image_Adapter_Abstract
 * 
 * an adapter for images
 *
 * @category   Naneau
 * @package    Naneau_Images
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
abstract class Naneau_Image_Adapter_Abstract {

    /**
     * image file location
     *
     * @var string
     */
    protected $_fileName;
    
    /**
     * mime type
     *
     * @var string
     */
    protected $_mimeType = 'image/jpeg';

    /**
     * image handle
     *
     * @var resource
     */
    protected $_handle;

    /**
     * constructor
     * @param string $fileName
     */
    public function __construct($fileName, $mimeType) {
        $this->_fileName = $fileName;
        //remember file name
        $this->_mimeType = $mimeType;
        //and the mime type
        $this->processFile();
        //process the file (create handle)
    }
    
    /**
     * get a thumbnail (using original image proportions)
     * 
     * $size is width the image will be resized to, the height of the resulting
     * image may vary (depending on the proportions)
     * 
     * it's essentially a wrapper around resize() that does some calculations on
     * the aspect ratio
     *
     * @param int $size
     * @param int $resizeHeight resize height to $size, not width
     */
    public function createThumbNail($size, $resizeHeight = false) {
        if ($resizeHeight) {
            //height is $size
            $height = $size;
            $factor = $this->getHeight() / $size;
            $width = round($this->getWidth() / $factor);
        }
        else {
            //width is $size
            $width = $size;
            $factor = $this->getWidth() / $size;
            $height = round($this->getHeight() / $factor);
        }
        //calculate height and width (using factor)

        $this->resize($width, $height);
        //actual resize
    }
    
    /**
     * save the image to a file
     * @param string $fileName
     */
    public function save($fileName = null, $type = null) {
        if ($fileName == null) {
            //save to original file
            $fileName = $this->_fileName;
        }
        //if no filename is given use original filename (overwrite)

        if ($type == null) {
            $type = Naneau_Image::TYPE_JPEG;
        }
        //jpeg by default

        $success = $this->_save($fileName, $type);
        //try to save the image

        if (!$success) {
            throw new Exception('file could not be saved');
        }
    }
    
    /**
     * process the file (create the handle)
     * @return void
     */
    abstract protected function processFile();

    /**
     * get the width of the image
     * @return int
     */
    abstract public function getWidth();

    /**
     * get the height of the image
     * @return int
     */
    abstract public function getHeight();

    /**
     * resize the image
     *
     * @param int $x
     * @param int $y
     * @return void
     */
    abstract public function resize($x, $y);

    /**
     * crop the image
     *
     * @param int $x
     * @param int $y
     * @param int $width
     * @param int $height
     */
    abstract public function crop($x, $y, $width, $height);
    
    /**
     * watermark the image, using a watermark file
     *
     * @param string $watermarFile
     * @return void
     */
    abstract public function waterMark($watermarFile);

    /**
     * save the image to a file
     * @param string|bool $fileName
     * @param int $type
     */
    abstract public function _save($fileName, $type);

    /**
     * get the image as a string
     * @param int $type
     * @return string
     */
    abstract public function getString($type);

}